package com.gujiayue.module.sys.pojo;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.google.common.collect.Lists;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SysUsrDo implements UserDetails, Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "主键")
	private int id;

	@ApiModelProperty(value = "用户主键")
	private String userId;

	@ApiModelProperty(value = "登录名")
	private String loginName;

	@ApiModelProperty(value = "密码")
	private String password;

	@ApiModelProperty(value = "登录邮箱")
	private String email;
	
	@ApiModelProperty(value = "登录手机")
	private String mobile;

	@ApiModelProperty(value = "用户角色")
	private List<SysRoleDo> roleList;

	@ApiModelProperty(value = "账户是否过期")
	private String accountNonExpired;

	@ApiModelProperty(value = "账户是否锁定")
	private String accountNonLocked;

	@ApiModelProperty(value = "密码是否过期")
	private String credentialsNonExpired;

	@ApiModelProperty(value = "账户是否可用")
	private String enabled;

	@ApiModelProperty(value = "是否单点登录")
	private String isSingle;

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> authorities = Lists.newArrayList();
		for (SysRoleDo role :getRoleList()) {
			authorities.add(new SimpleGrantedAuthority(role.getCode()));
		}
		return authorities;
	}

	@Override
	public String getUsername() {
		return getLoginName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return "Y".equals(getAccountNonLocked());
	}

	@Override
	public boolean isAccountNonLocked() {
		return "Y".equals(getAccountNonLocked());
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return "Y".equals(getCredentialsNonExpired());
	}

	@Override
	public boolean isEnabled() {
		return "Y".equals(getEnabled());
	}
}
